package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketComment;
import com.cskaoyan.bean.pojo.MarketCommentExample;
import com.cskaoyan.bean.pojo.MarketGoodsExample;
import com.cskaoyan.bean.pojo.MarketOrder;
import com.cskaoyan.bean.vo.CommentListVO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author rebaogu
 * @description
 * @date 2022.04.07 23:07
 * @package com.cskaoyan.service.impl
 * @Classname CommentServiceImpl
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    // TODO 实现 查看商品评论列表 与 部分查询
    @Override
    public CommentListVO commentList(BaseParam param, Integer userId, Integer goodsId) {

        // 获取分页相关信息
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();

        // 开启分页
        PageHelper.startPage(page, limit);


        MarketCommentExample example = new MarketCommentExample();

        MarketCommentExample.Criteria criteria = example.createCriteria();

        // 拼接 顺序
        example.setOrderByClause(sort + " " + order);

        // deteid 为0
        criteria.andDeletedLessThanOrEqualTo(false);

        // 如果不为空,就拼接
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }

        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andValueIdEqualTo(goodsId);
        }

        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);

        // 插件获取分页信息
        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        int pages = pageInfo.getPages();

        return new CommentListVO(total, pages, limit, page, marketComments);
    }

    // todo 实现 形式上的删除 deteid --> false --> true
    @Override
    public void commentDelete(MarketComment marketComment) {
        Integer id = marketComment.getId();
        marketComment.setDeleted(true);

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andIdEqualTo(id);

        marketCommentMapper.updateByExampleSelective(marketComment, marketCommentExample);

    }
}
